package cn.afactory.tygz.ui.fragments;

import android.graphics.Color;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.lidroid.xutils.util.LogUtils;
import com.squareup.leakcanary.RefWatcher;

import java.lang.reflect.Field;

import cn.afactory.tygz.R;
import cn.afactory.tygz.TianYaGongZaiApplication;
import cn.afactory.tygz.common.AppDebug;
import cn.afactory.tygz.common.ErrorCode;
import cn.afactory.tygz.ui.base.utils.LocalUtils;

public abstract class BaseTabFragment extends BaseFragment {

	private LinearLayout mlayout;


    /**
     * 异常提示
     */
	private LinearLayout top_net_error_msg;
	private LinearLayout ll_net_error;
    private LinearLayout empty_message_layout;
    private LinearLayout ll_source_error;
    private Button btn_refresh_data;
    private Button btn_refresh_net;

    private Boolean NeedErrorMsg = false;
	private LayoutInflater minflater;

	private FrameLayout rl_action_bar;
	private TextView titleView;
	private ImageView leftIcon;
	private ImageView rightIcon;
	private Button title_bar_btn_left;
	private Button title_bar_btn_right;
//	protected SearchNavigationView mSearchNavigationView;

	private ViewGroup ll_title_panel;

	private TextView title_bar_btn_left_more;

    public BaseTabFragment() {

    }

//	private View rootView;//缓存Fragment view
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		this.minflater = inflater;
		mlayout = (LinearLayout) inflater.inflate(
				R.layout.base_fragment_layout, null);

		init();
		LogUtils.d("View debug onCreateView");
		LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(
				LinearLayout.LayoutParams.MATCH_PARENT,
				LinearLayout.LayoutParams.MATCH_PARENT);
		((LinearLayout) mlayout.findViewById(R.id.ll_content)).addView(
				onCreateViewCustom(inflater, container, savedInstanceState), param);

		return mlayout;
	}

	/**
	 * @Description 子类中使用onCreateView
	 * @author xiezhidong@pajk.cn
	 * @return
	 */

	public abstract View onCreateViewCustom(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState);

	public void init() {
		if (mlayout == null)
			return;

		rl_action_bar = (FrameLayout)mlayout.findViewById(R.id.rl_action_bar);
		titleView = (TextView) mlayout.findViewById(R.id.title_bar_name);
        leftIcon = (ImageView) mlayout.findViewById(R.id.title_bar_iv_left);
        leftIcon.setVisibility(View.GONE);
        rightIcon = (ImageView) mlayout.findViewById(R.id.title_bar_iv_right);
        title_bar_btn_left = (Button) mlayout.findViewById(R.id.title_bar_btn_left);
        title_bar_btn_right = (Button) mlayout.findViewById(R.id.title_bar_btn_right);
        title_bar_btn_left_more = (TextView) mlayout.findViewById(R.id.title_bar_btn_left_more);

		// 异常处理
		// 异常处理
		ll_source_error = (LinearLayout) mlayout
				.findViewById(R.id.ll_source_error_fragment);
		ll_net_error = (LinearLayout) mlayout.findViewById(R.id.ll_net_error_base);
		top_net_error_msg = (LinearLayout) mlayout
				.findViewById(R.id.top_net_error_msg_base);
		empty_message_layout = (LinearLayout) mlayout
				.findViewById(R.id.empty_message_layout_base);
		btn_refresh_data = (Button) mlayout.findViewById(R.id.btn_refresh_data_base);
		btn_refresh_net = (Button) mlayout.findViewById(R.id.btn_refresh_net_base);

//		mSearchNavigationView= (SearchNavigationView) mlayout.findViewById(R.id.search_nav_view);
	}

	public void setTitleColor(String color) {
//		FrameLayout frame =  (FrameLayout)ll_titlebar.findViewById(R.id.rl_action_bar);
//		frame.setBackgroundColor(color);
		rl_action_bar.setBackgroundColor(Color.parseColor(color));
	}
	/**
	 * 获取标题栏的高度
	 * @return
	 */
	public int getTitleBarHeight(){
		return rl_action_bar.getHeight();
	}
	/**
	 * 获取状态栏的高度
	 * @return
	 */
	public int getStatusBarHeight(){
		try{
			Class c = Class.forName("com.android.internal.R$dimen");
			Object obj = c.newInstance();
			Field field = c.getField("status_bar_height");
			int x = Integer.parseInt(field.get(obj).toString());
			int y = getResources().getDimensionPixelSize(x);
			return y;
		}catch(Exception e){
			e.printStackTrace();
		}
		return 0;
	}

    public void setTitle(String title) {
		rl_action_bar.setVisibility(View.VISIBLE);
        titleView.setText(title);
    }

    public void setTitle(int resId) {
		rl_action_bar.setVisibility(View.VISIBLE);
        titleView.setText(getString(resId));
    }

    public void setLeftIcon(int resId,OnClickListener lsn){
    	if(leftIcon == null){
    		return;
    	}
    	leftIcon.setVisibility(View.VISIBLE);
    	leftIcon.setImageResource(resId);
    	if(lsn == null){
    		return ;
    	}
    	leftIcon.setOnClickListener(lsn);
    }
	public void setLeftIcon(int resId,OnClickListener lsn, int backgroundId){
		if(leftIcon == null){
			return;
		}
		leftIcon.setVisibility(View.VISIBLE);
		leftIcon.setImageResource(resId);
		leftIcon.setBackgroundResource(backgroundId);
		if(lsn == null){
			return ;
		}
		leftIcon.setOnClickListener(lsn);
	}
	public void hideLeftIcon(){
    	if(leftIcon == null){
    		return;
    	}
    	leftIcon.setVisibility(View.GONE);
    }
    
    public void setLeftButtonMore(String str,OnClickListener lsn){
    	if(title_bar_btn_left_more == null){
    		return;
    	}
    	title_bar_btn_left_more.setVisibility(View.VISIBLE);
    	if(lsn == null){
    		return ;
    	}
    	title_bar_btn_left_more.setOnClickListener(lsn);
    	if(!TextUtils.isEmpty(str)) {
    		title_bar_btn_left_more.setText(str);
    	}else{
			title_bar_btn_left_more.setVisibility(View.INVISIBLE);
		}
    }

	public void setRightButton(String str,OnClickListener lsn){
		if(title_bar_btn_right == null){
			return ;
		}

		title_bar_btn_right.setVisibility(View.VISIBLE);
		title_bar_btn_right.setText(str);
		title_bar_btn_right.setOnClickListener(lsn);
	}

	public void hideRightButton(){
		title_bar_btn_right.setVisibility(View.GONE);
	}
    
    public void hideLeftButtonMore(){
    	if(title_bar_btn_left_more == null){
    		return;
    	}
    	title_bar_btn_left_more.setVisibility(View.GONE);
    }
    
    public void setRightIcon(int resId,OnClickListener lsn){
    	if(rightIcon == null){

    		return;
    	}
    	rightIcon.setVisibility(View.VISIBLE);
    	rightIcon.setImageResource(resId);
    	if(lsn == null){
    		return ;
    	}
    	rightIcon.setOnClickListener(lsn);
    }

	public void setRightIcon(int resId,OnClickListener lsn, int backgroundId){
		if(rightIcon == null){
			return;
		}
		rightIcon.setVisibility(View.VISIBLE);
		rightIcon.setImageResource(resId);


		rightIcon.setBackgroundResource(backgroundId);

		if(lsn == null){
			return ;
		}
		rightIcon.setOnClickListener(lsn);
	}

    public void hideRightIcon(){
    	if(rightIcon == null){
    		return;
    	}
    	rightIcon.setVisibility(View.GONE);
    }

    public Button getTitleLeftButton(){
    	return title_bar_btn_left;
    }

    public Button getTitleRightButton(){
    	return title_bar_btn_right;
    }
    /**
     * @Description 显示空页面
     */

    public void showNullPage() {
        empty_message_layout.setVisibility(View.VISIBLE);
    }

    public void hideNullPage() {
        empty_message_layout.setVisibility(View.GONE);
    }

    /**
     * @Description 显示错误页面
     * @author xiezhidong@pajk.cn
     * @param errorCode 参考  ErrorCode
     * @param listener
     */

    public void showErrorPage(int errorCode, OnClickListener listener) {
        switch (errorCode) {
        case ErrorCode.STATUS_IO_EXCEPTION:
            showHttpDataError(listener);
            break;
        case ErrorCode.STATUS_NETWORK_EXCEPTION:
            showNetWorkError(1, listener);
            break;
        case ErrorCode.CONNECTTION_TIME_OUT:
            showNetWorkError(1, listener);
            break;
        case ErrorCode.DEVICE_TOKEN_MISSING:
        	LocalUtils.showToast(getActivity(), "DEVICE_TOKEN_MISSING");
            break;

        case ErrorCode.NETWORK_UNAVAILABLE:
            showNetWorkError(0, listener);
            break;

        case ErrorCode.NOT_LOGIN:
        	LocalUtils.showToast(getActivity(), "NOT_LOGIN");
            break;

        case ErrorCode.NO_RECOMMAND_DOCTORS:
        	LocalUtils.showToast(getActivity(), "NO_RECOMMAND_DOCTORS");
            break;

        case ErrorCode.NO_GET_DOCTOR_INFO:
        	LocalUtils.showToast(getActivity(), "NO_GET_DOCTOR_INFO");
            break;

        default:

            break;
        }
    }

    /**
     * @Description
     * @author xiezhidong@pajk.cn
     * @param errorCode  -1 为隐藏所有
     */

    public void hideErrorPage(int errorCode) {
        switch (errorCode) {
        case ErrorCode.STATUS_IO_EXCEPTION:
            hideHttpDataError();
            break;
        case ErrorCode.STATUS_NETWORK_EXCEPTION:
            hideNetWorkError();
            break;
        case ErrorCode.CONNECTTION_TIME_OUT:
            hideNetWorkError();
            break;
        case ErrorCode.DEVICE_TOKEN_MISSING:
            break;

        case ErrorCode.NETWORK_UNAVAILABLE:
            hideNetWorkError();
            break;

        case ErrorCode.NOT_LOGIN:
            break;

        case ErrorCode.NO_RECOMMAND_DOCTORS:
            break;

        case ErrorCode.NO_GET_DOCTOR_INFO:
            break;

        default:
			hideNullPage();
        	hideNetWorkError();
        	hideHttpDataError();
            break;
        }
    }

    /**
     * 服务端获取数据异常
     */
    public void showHttpDataError(OnClickListener clickListener) {

        ll_source_error.setVisibility(View.VISIBLE);
        btn_refresh_data.setOnClickListener(clickListener);
    }

    public void hideHttpDataError() {
        ll_source_error.setVisibility(View.GONE);
    }

//    /**
//     * 头部显示网络异常
//     */
//    public void showNetWorkErrorOnTop(){
//		if(top_net_error_msg != null && top_net_error_msg.getVisibility() == View.GONE){
//			top_net_error_msg.setVisibility(View.VISIBLE);
//		}
//    }
//
//    public void hideNetWorkErrorOnTop(){
//		if(top_net_error_msg != null && top_net_error_msg.getVisibility() == View.VISIBLE){
//			top_net_error_msg.setVisibility(View.GONE);
//		}
//    }

    /**
     * @Description  网络异常提示
     * @author xiezhidong@pajk.cn
     * @param listener
     * @param i 0 == NETWORK_UNAVAILABLE, 1 == STATUS_NETWORK_EXCEPTION
     */

    public void showNetWorkError(int i, OnClickListener listener) {
        if(mContext == null){
            return ;
        }
		ll_net_error.setVisibility(View.VISIBLE);
		if (i == 0) {
			((TextView) (ll_net_error.findViewById(R.id.tv_network_error)))
					.setText(mContext.getString(R.string.recheck_textview_no_network));
		} else if (i == 1) {
			((TextView) (ll_net_error.findViewById(R.id.tv_network_error)))
                    .setText(mContext.getString(R.string.recheck_textview));
		}

		btn_refresh_net.setOnClickListener(listener);
    }

    public void hideNetWorkError() {
        ll_net_error.setVisibility(View.GONE);
    }

	public void showDefaultBackView() {
		setLeftIcon(R.drawable.arrow_back, new OnClickListener() {
			@Override
			public void onClick(View v) {
				if (mActivity != null) {
					mActivity.finish();
				}
			}
		});
	}

	/**
	 * @param show
	 */
	public void showTitleBar(boolean show) {
		rl_action_bar.setVisibility(show ? View.VISIBLE : View.GONE);
	}

	@Override
	public void onDestroy() {
		if (mUIHandler != null) {
			mUIHandler.setValid(true);
		}
		//内容泄露检测
		if(AppDebug.MEMEORY_LEAK_CAP_DEBUG) {
			RefWatcher refWatcher = TianYaGongZaiApplication.getRefWatcher(getActivity());
			refWatcher.watch(this);
		}
		super.onDestroy();
	}
}
